/**
 * "Visual Paradigm: DO NOT MODIFY THIS FILE!"
 * 
 * This is an automatic generated file. It will be regenerated every time 
 * you generate persistence class.
 * 
 * Modifying its content may cause the program not work, or your work may lost.
 */

using System;
using Orm;
using System.Collections;
using NHibernate;

namespace petshop {
	/// <summary>
	/// ORM-Persistable Class
	/// </summary>
	public class CustomerFactory {
		public static global::petshop.Customer LoadCustomerByORMID(int ID) {
			PersistentSession session = global::petshop.PetShopPersistentManager.Instance().GetSession();
			return LoadCustomerByORMID(session,ID);
		}
		
		public static global::petshop.Customer LoadCustomerByORMID(PersistentSession session,int ID) {
			return (global::petshop.Customer) session.Load(typeof(global::petshop.Customer), (Int32)ID);
		}
		
		public static global::petshop.Customer[] ListCustomerByQuery(string condition, string orderBy) {
			PersistentSession session = global::petshop.PetShopPersistentManager.Instance().GetSession();
			return ListCustomerByQuery(session, condition, orderBy);
		}
		
		public static global::petshop.Customer[] ListCustomerByQuery(PersistentSession session, string condition, string orderBy) {
			global::System.Text.StringBuilder sb = new global::System.Text.StringBuilder("From petshop.Customer as Customer");
			if (condition != null) {
				sb.Append(" Where ");
				sb.Append(condition);
			}
			if (orderBy != null) {
				sb.Append(" Order By ");
				sb.Append(orderBy);
			}
			IQuery query = session.CreateQuery(sb.ToString());
			try {
				IList list = query.List();
				global::petshop.Customer[] result = new global::petshop.Customer[list.Count];
				list.CopyTo(result, 0);
				return result;
			}
			catch (Exception e) {
				global::System.Console.Error.WriteLine(e.Message);
				global::System.Console.Error.WriteLine(e.StackTrace);
				throw new PersistentException(e);
			}
		}
		
		public static global::petshop.Customer LoadCustomerByQuery(string condition, string orderBy) {
			PersistentSession session = global::petshop.PetShopPersistentManager.Instance().GetSession();
			return LoadCustomerByQuery(session, condition, orderBy);
		}
		
		public static global::petshop.Customer LoadCustomerByQuery(PersistentSession session, string condition, string orderBy) {
			global::petshop.Customer[] customers = ListCustomerByQuery(session, condition, orderBy);
			if (customers != null && customers.Length > 0)
				return customers[0];
			else
				return null;
		}
		
		public static global::System.Collections.IEnumerable IterateCustomerByQuery(string condition, string orderBy) {
			PersistentSession session = global::petshop.PetShopPersistentManager.Instance().GetSession();
			return IterateCustomerByQuery(session, condition, orderBy);
		}
		
		public static global::System.Collections.IEnumerable IterateCustomerByQuery(PersistentSession session, string condition, string orderBy) {
			global::System.Text.StringBuilder sb = new global::System.Text.StringBuilder("From petshop.Customer as Customer");
			if (condition != null) {
				sb.Append(" Where ");
				sb.Append(condition);
			}
			if (orderBy != null) {
				sb.Append(" Order By ");
				sb.Append(orderBy);
			}
			IQuery query = session.CreateQuery(sb.ToString());
			try {
				return query.Enumerable();
			}
			catch (Exception e) {
				global::System.Console.Error.WriteLine(e.Message);
				global::System.Console.Error.WriteLine(e.StackTrace);
				throw new PersistentException(e);
			}
		}
		
		public static Customer CreateCustomer() {
			return new global::petshop.Customer();
		}
		
		public Customer LoadCustomerByCriteria(CustomerCriteria customerCriteria) {
			Customer[] customers = ListCustomerByCriteria(customerCriteria);
			if(customers == null || customers.Length == 0) {
				return null;
			}
			return customers[0];
		}
		
		public Customer[] ListCustomerByCriteria(CustomerCriteria customerCriteria) {
			return customerCriteria.ListCustomer();
		}
	}
	
}
